package view.principal;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import main.Main;
import vacuumAgent.environment.VAEnvironment;
import view.EnvironmentDrawPanel.FloorPanel;
import exception.VAIllegalMove;

public class StartActionListener implements ActionListener, Runnable
{

	Main frame;
	int step;
	
	public StartActionListener( Main frame, int step ) {
		this.frame = frame;
		this.step = step;		
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		FloorPanel floor = ( FloorPanel ) frame.getContentPane();
		floor.setEditable( false );
		frame.getSave().setEnabled( false );
		Thread t = new Thread( this );
		t.start();
	}

	@Override
	public void run() {
		VAEnvironment environment = frame.getEnvironment();
		try 
		{
			if( step == 0 )
			{
				while( !environment.isDone() )
				{
					environment.step( 1 );
					FloorPanel panel = ( FloorPanel )frame.getContentPane();
					panel.repaintFloor( environment.getVacuumAgent().getEnergy(), environment.getPerformanceMeasure() );
				}
			}
			else
			{				
				environment.step( step );
				FloorPanel panel = ( FloorPanel )frame.getContentPane();
				panel.repaintFloor( environment.getVacuumAgent().getEnergy(), environment.getPerformanceMeasure() );
			}
		} catch ( VAIllegalMove e ) {
			e.printStackTrace();
		} catch ( Exception e )
		{
			e.printStackTrace();
		}
	}	
}
